using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using mojoPortal.Web;
using Resources;
using ud_ImageSlideShow.Data;

namespace ud_ImageSlideShow
{
    public partial class ImageSlideShowModule : SiteModuleControl
    {
        private ImageSlideShowConfiguration config = new ImageSlideShowConfiguration();
        readonly DBImageSlideShow repository = new DBImageSlideShow();
        private string[] listModuleId;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += PageLoad;
            EnableViewState = false;
        }

        protected virtual void PageLoad(object sender, EventArgs e)
        {
            LoadSettings();
            SetupScript();
            PopulateLabels();
            PopulateControls();
        }

        private void SetupScript()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>");
            if (config.ItemVisibleInCallbackSetting != string.Empty)
            {
                sb.Append("function " + config.ItemVisibleInCallbackSetting + "BeforeAnimation(carousel, item, idx, state) {");
                sb.Append("if (state == 'init') return;");
                sb.Append("};");

                sb.Append("function " + config.ItemVisibleInCallbackSetting + "AfterAnimation(carousel, item, idx, state) {");
                sb.Append("if (state == 'init') return;");
                sb.Append("};");
            }
            if (config.ItemVisibleOutCallbackSetting != string.Empty)
            {
                sb.Append("function " + config.ItemVisibleOutCallbackSetting + "BeforeAnimation(carousel, item, idx, state) {");
                sb.Append("if (state == 'init') return;");
                sb.Append("$('.module" + ModuleId + " .imgWrapper').animate({opacity: 0.5},500);");
                sb.Append("$('.module" + ModuleId + " .imgWrapper').html($(item).html());");
                sb.Append("$('.module" + ModuleId + " .imgWrapper').animate({opacity: 1},500);");
                sb.Append("};");

                sb.Append("function " + config.ItemVisibleOutCallbackSetting + "AfterAnimation(carousel, item, idx, state) {");
                sb.Append("if (state == 'init') return;");
                sb.Append("};");

                sb.Append("$(document).ready(function(){");
                sb.Append("$('.module" + ModuleId + " .imgWrapper').html($('.module" + ModuleId + " ul li:first').html());");
                sb.Append("$('.module" + ModuleId + " .mainscroll ul li').click(function(){");
                sb.Append("$('.module" + ModuleId + " .imgWrapper').animate({opacity: 0.5},500);");
                sb.Append("$('.module" + ModuleId + " .imgWrapper').html($(this).html());");
                sb.Append("$('.module" + ModuleId + " .imgWrapper').animate({opacity: 1},500);");
                sb.Append("});");
                sb.Append("});");
            }
            sb.Append("</script>");
            if(!Page.ClientScript.IsStartupScriptRegistered(typeof(Page), "carousel" + ModuleId))
            {
                Page.ClientScript.RegisterStartupScript(typeof(Page), "carousel" + ModuleId, sb.ToString());
            }
        }

        private void PopulateControls()
        {
            BindImages();
            carousel.Animation = config.AnimationSetting;
            carousel.Auto = config.AutoSetting;
            carousel.Easing = config.EasingSetting;
            carousel.ItemVisibleInCallback = config.ItemVisibleInCallbackSetting;
            carousel.ItemVisibleOutCallback = config.ItemVisibleOutCallbackSetting;
            carousel.Offset = config.OffsetSetting;
            carousel.RightToLeft = config.UseRightToLeftSetting;
            carousel.Scroll = config.ScrollItemSetting;
            carousel.Size = config.SizeSetting;
            carousel.Start = config.StartSetting;
            carousel.Vertical = config.UseVerticalSetting;
            carousel.WrapMode = config.WrapSetting;
            
        }

        private void BindImages()
        {
            if (listModuleId == null)
            {
                listModuleId = new[] {string.Empty};
            }
            rptItems.DataSource = config.NoInvidualModule ? repository.GetAll(listModuleId) : repository.GetAll(ModuleId);
            rptItems.DataBind();
        }

        protected virtual void PopulateLabels()
        {
            Title1.EditText = ImageSlideShowResource.ButtonEdit;
            Title1.EditUrl = SiteRoot + "/ImageSlideShow/Admin/ImageSlideShowEdit.aspx";
            pnlImgWrapper.Visible = config.ShowImageWrapSetting;
            uljcarousel.Attributes["class"] = config.CustomCssCarousel != string.Empty
                                                  ? "jcarousel-skin-" + config.CustomCssCarousel
                                                  : "jcarousel-skin-tango";
        }

        protected virtual void LoadSettings()
        {
            pnlContainer.ModuleId = ModuleId;
            config = new ImageSlideShowConfiguration(Settings);
            if (!config.CustomCssClass.Equals(string.Empty)) { pnlContainer.CssClass += " " + config.CustomCssClass; }
            string idAll = config.ModuleLoader;
            if (idAll == string.Empty) return;
            char[] param = {';'};
            listModuleId = idAll.Contains(";") ? idAll.Split(param) : new[] {idAll};
        }

    }
}
